package com.roc.steelend.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.roc.steelend.controller.vo.account.AccountSpecialBuyOrderIdDetail;
import com.roc.steelend.controller.vo.account.AccountSpecialKaipingOrderIdDetail;
import com.roc.steelend.controller.vo.account.AccountSpecialSaleOrderIdDetail;
import com.roc.steelend.controller.vo.invoice.FindInvoiceDataReception;
import com.roc.steelend.controller.vo.invoice.InvoiceByBuyOrder;
import com.roc.steelend.controller.vo.invoice.InvoiceByKaipingOrder;
import com.roc.steelend.controller.vo.invoice.InvoiceBySaleOrder;
import com.roc.steelend.controller.vo.returnformat.ReturnMsg;
import com.roc.steelend.controller.vo.returnformat.ReturnMsgForTable;
import com.roc.steelend.mapper.*;
import com.roc.steelend.pojo.*;
import com.roc.steelend.service.InvoiceService;
import com.roc.steelend.util.BuyOrderByAccountComparator;
import com.roc.steelend.util.KaipingOrderByAccountComparator;
import com.roc.steelend.util.LshUtil;
import com.roc.steelend.util.SaleOrderByAccountComparator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class InvoiceServiceImpl  implements InvoiceService {

    @Autowired
    private InvoiceMapper invoiceMapper;

    @Autowired
    private InvoiceDetailBuyMapper invoiceDetailBuyMapper;

    @Autowired
    private SuppliersMapper suppliersMapper;


    @Autowired
    private BuyOrderMapper buyOrderMapper;

    @Autowired
    private SaleOrderMapper saleOrderMapper;
    @Autowired
    private ClientMapper clientMapper;

    @Autowired
    private InvoiceDetailSaleMapper invoiceDetailSaleMapper;

    @Autowired
    private KaipingGuyMapper kaipingGuyMapper;
    @Autowired
    private KaipingOrderMapper kaipingOrderMapper;
    @Autowired
    private InvoiceDetailKaipingMapper invoiceDetailKaipingMapper;


    @Override
    public ReturnMsg invoiceBuy(InvoiceByBuyOrder invoiceByBuyOrder, Integer employeeId) {
        ReturnMsg msg = new ReturnMsg();

        //        生成一个invoice数据
        Invoice invoice = new Invoice();
        BigInteger invoiceBuyLsh = LshUtil.getInvoiceBuy();
        invoice.setInvoiceId(invoiceBuyLsh);
        invoice.setEmployeeId(employeeId);
        invoice.setInvoiceTime(invoiceByBuyOrder.getInvoiceTime());
        invoice.setOperateTime(new Timestamp(new Date().getTime()));
        invoice.setInvoiceType("采购开票");
        invoice.setInvoiceMoney(invoiceByBuyOrder.getInvoiceMoney());
        invoice.setWho(invoiceByBuyOrder.getSupplierName());
        invoice.setRemark(invoiceByBuyOrder.getRemark());
        invoice.setInvoiceTitle(invoiceByBuyOrder.getInvoiceTitle());
        int invoiceInsert = invoiceMapper.insert(invoice);
        if (invoiceInsert==0){
            msg.setCode(0);
            msg.setMsg("开票操作失败,请重试");
            return msg;
        }

//        不抹零的情况
        if (!invoiceByBuyOrder.getIsOver()){
//            操作供应商表
            QueryWrapper<Suppliers> suppliersQueryWrapper = new QueryWrapper<>();
            suppliersQueryWrapper.eq("supplier_id",invoiceByBuyOrder.getSupplierId());
            Suppliers suppliers = suppliersMapper.selectList(suppliersQueryWrapper).get(0);
            BigDecimal invoice1 = suppliers.getInvoice();
            suppliers.setInvoice(invoice1.subtract(invoiceByBuyOrder.getInvoiceMoney()));
            suppliersMapper.updateById(suppliers);
//            操作开票细节表和采购订单表
            List<AccountSpecialBuyOrderIdDetail> buyOrderIdList = invoiceByBuyOrder.getBuyOrderIdList();
            Collections.sort(buyOrderIdList,new BuyOrderByAccountComparator());
            if (Objects.equals(invoiceByBuyOrder.getInvoiceMoney(),invoiceByBuyOrder.getShouldInvoiceMoney())){
//            便捷开票
                for (int i = 0; i <buyOrderIdList.size() ; i++) {
//                    操作采购订单
                    BigInteger buyOrderId = buyOrderIdList.get(i).getBuyOrderId();
                    QueryWrapper<BuyOrder> buyOrderQueryWrapper  = new QueryWrapper<>();
                    buyOrderQueryWrapper.eq("buy_order_id",buyOrderId);
                    BuyOrder buyOrder = buyOrderMapper.selectList(buyOrderQueryWrapper).get(0);
                    BigDecimal tempInvoiceMoney = buyOrder.getInvoiceMoney();
                    buyOrder.setInvoiceMoney(new BigDecimal("0"));
                    buyOrder.setInvoiceIsSettled(1);
                    buyOrder.setIsInvoice(1);
                    buyOrderMapper.updateById(buyOrder);
//                    操作开票细节
                    InvoiceDetailBuy invoiceDetailBuy = new InvoiceDetailBuy();
                    invoiceDetailBuy.setInvoiceId(invoiceBuyLsh);
                    invoiceDetailBuy.setBuyOrderId(buyOrderId);
                    invoiceDetailBuy.setInvoiceOver(0);
                    invoiceDetailBuy.setInvoiceDetailMoney(tempInvoiceMoney);
                    invoiceDetailBuyMapper.insert(invoiceDetailBuy);

                }
            }else {
//              普通开票，
                BigDecimal invoiceMoney = invoiceByBuyOrder.getInvoiceMoney();
                for (int i = 0; i <buyOrderIdList.size() ; i++) {
                    BigInteger buyOrderId = buyOrderIdList.get(i).getBuyOrderId();
                    QueryWrapper<BuyOrder> buyOrderQueryWrapper = new QueryWrapper<>();
                    buyOrderQueryWrapper.eq("buy_order_id",buyOrderId);
                    BuyOrder buyOrder = buyOrderMapper.selectList(buyOrderQueryWrapper).get(0);
                    BigDecimal shouldInvoiceMoney = buyOrder.getInvoiceMoney();
                    if (shouldInvoiceMoney.compareTo(invoiceMoney)==0){
                        buyOrder.setInvoiceMoney(new BigDecimal("0"));
                        buyOrder.setInvoiceIsSettled(1);
                        buyOrder.setIsInvoice(1);
                        buyOrderMapper.updateById(buyOrder);
                        InvoiceDetailBuy invoiceDetailBuy = new InvoiceDetailBuy();
                        invoiceDetailBuy.setInvoiceId(invoiceBuyLsh);
                        invoiceDetailBuy.setBuyOrderId(buyOrderId);
                        invoiceDetailBuy.setInvoiceDetailMoney(invoiceMoney);
                        invoiceDetailBuy.setInvoiceOver(0);
                        invoiceDetailBuyMapper.insert(invoiceDetailBuy);
                        break;
                    }else if (shouldInvoiceMoney.compareTo(invoiceMoney)==-1){
                        invoiceMoney=invoiceMoney.subtract(shouldInvoiceMoney);
                        buyOrder.setInvoiceMoney(new BigDecimal("0"));
                        buyOrder.setInvoiceIsSettled(1);
                        buyOrder.setIsInvoice(1);
                        buyOrderMapper.updateById(buyOrder);
                        InvoiceDetailBuy invoiceDetailBuy = new InvoiceDetailBuy();
                        invoiceDetailBuy.setInvoiceId(invoiceBuyLsh);
                        invoiceDetailBuy.setBuyOrderId(buyOrderId);
                        invoiceDetailBuy.setInvoiceDetailMoney(shouldInvoiceMoney);
                        invoiceDetailBuy.setInvoiceOver(0);
                        invoiceDetailBuyMapper.insert(invoiceDetailBuy);
                        continue;
                    }else if (shouldInvoiceMoney.compareTo(invoiceMoney)==1){
                        buyOrder.setInvoiceMoney(buyOrder.getInvoiceMoney().subtract(invoiceMoney));
                        buyOrder.setIsInvoice(1);
                        buyOrderMapper.updateById(buyOrder);
                        InvoiceDetailBuy invoiceDetailBuy = new InvoiceDetailBuy();
                        invoiceDetailBuy.setInvoiceId(invoiceBuyLsh);
                        invoiceDetailBuy.setBuyOrderId(buyOrderId);
                        invoiceDetailBuy.setInvoiceDetailMoney(invoiceMoney);
                        invoiceDetailBuy.setInvoiceOver(0);
                        invoiceDetailBuyMapper.insert(invoiceDetailBuy);
                        break;
                    }
                }
            }
        }else {
//                 选择结算完毕的情况
            QueryWrapper<Suppliers> suppliersQueryWrapper = new QueryWrapper<>();
            suppliersQueryWrapper.eq("supplier_id",invoiceByBuyOrder.getSupplierId());
            Suppliers suppliers = suppliersMapper.selectList(suppliersQueryWrapper).get(0);
            BigDecimal invoice1 = suppliers.getInvoice();
            suppliers.setInvoice(invoice1.subtract(invoiceByBuyOrder.getShouldInvoiceMoney()));
            suppliersMapper.updateById(suppliers);
            List<AccountSpecialBuyOrderIdDetail> buyOrderIdList = invoiceByBuyOrder.getBuyOrderIdList();
            Collections.sort(buyOrderIdList,new BuyOrderByAccountComparator());
            for (int i = 0; i <buyOrderIdList.size() ; i++) {
                BigInteger buyOrderId = buyOrderIdList.get(i).getBuyOrderId();
                QueryWrapper<BuyOrder> buyOrderQueryWrapper = new QueryWrapper<>();
                buyOrderQueryWrapper.eq("buy_order_id",buyOrderId);
                BuyOrder buyOrder = buyOrderMapper.selectList(buyOrderQueryWrapper).get(0);
                BigDecimal invoiceRealMoney = buyOrder.getInvoiceMoney();
                buyOrder.setInvoiceMoney(new BigDecimal("0"));
                buyOrder.setInvoiceIsSettled(1);
                buyOrder.setIsInvoice(1);
                buyOrderMapper.updateById(buyOrder);

                InvoiceDetailBuy invoiceDetailBuy = new InvoiceDetailBuy();
                invoiceDetailBuy.setInvoiceId(invoiceBuyLsh);
                invoiceDetailBuy.setBuyOrderId(buyOrderId);
                invoiceDetailBuy.setInvoiceDetailMoney(invoiceRealMoney);
                invoiceDetailBuy.setInvoiceOver(1);
                invoiceDetailBuyMapper.insert(invoiceDetailBuy);
            }
        }

        msg.setCode(1);
        msg.setMsg("采购开票操作成功");
        return msg;
    }

    @Override
    public ReturnMsg invoiceSale(InvoiceBySaleOrder invoiceBySaleOrder, Integer employeeId) {
        ReturnMsg msg = new ReturnMsg();

        //        生成一个invoice数据
        Invoice invoice = new Invoice();
        BigInteger invoiceSaleLsh = LshUtil.getInvoiceSale();
        invoice.setInvoiceId(invoiceSaleLsh);
        invoice.setEmployeeId(employeeId);
        invoice.setInvoiceTime(invoiceBySaleOrder.getInvoiceTime());
        invoice.setOperateTime(new Timestamp(new Date().getTime()));
        invoice.setInvoiceType("销售开票");
        invoice.setInvoiceMoney(invoiceBySaleOrder.getInvoiceMoney());
        invoice.setWho(invoiceBySaleOrder.getClientName());
        invoice.setRemark(invoiceBySaleOrder.getRemark());
        invoice.setInvoiceTitle(invoiceBySaleOrder.getInvoiceTitle());
        int invoiceInsert = invoiceMapper.insert(invoice);
        if (invoiceInsert==0){
            msg.setCode(0);
            msg.setMsg("开票操作失败,请重试");
            return msg;
        }

//        不抹零的情况
        if (!invoiceBySaleOrder.getIsOver()){
//            操作客户表
            QueryWrapper<Client> clientQueryWrapper = new QueryWrapper<>();
            clientQueryWrapper.eq("client_id",invoiceBySaleOrder.getClientId());
            Client client = clientMapper.selectList(clientQueryWrapper).get(0);
            BigDecimal invoice1 = client.getInvoice();
            client.setInvoice(invoice1.subtract(invoiceBySaleOrder.getInvoiceMoney()));
            clientMapper.updateById(client);
//            操作开票细节表和销售订单表
            List<AccountSpecialSaleOrderIdDetail> saleOrderIdList = invoiceBySaleOrder.getSaleOrderIdList();
            Collections.sort(saleOrderIdList,new SaleOrderByAccountComparator());
            if (Objects.equals(invoiceBySaleOrder.getInvoiceMoney(),invoiceBySaleOrder.getShouldInvoiceMoney())){
//            便捷开票
                for (int i = 0; i <saleOrderIdList.size() ; i++) {
//                    操作销售订单
                    BigInteger saleOrderId = saleOrderIdList.get(i).getSaleOrderId();
                    QueryWrapper<SaleOrder> saleOrderQueryWrapper  = new QueryWrapper<>();
                    saleOrderQueryWrapper.eq("sale_order_id",saleOrderId);
                    SaleOrder saleOrder = saleOrderMapper.selectList(saleOrderQueryWrapper).get(0);
                    BigDecimal tempInvoiceMoney = saleOrder.getInvoiceMoney();
                    saleOrder.setInvoiceMoney(new BigDecimal("0"));
                    saleOrder.setInvoiceIsSettled(1);
                    saleOrder.setIsInvoice(1);
                    saleOrderMapper.updateById(saleOrder);
//                    操作开票细节
                    InvoiceDetailSale invoiceDetailSale = new InvoiceDetailSale();
                    invoiceDetailSale.setInvoiceId(invoiceSaleLsh);
                    invoiceDetailSale.setSaleOrderId(saleOrderId);
                    invoiceDetailSale.setInvoiceOver(0);
                    invoiceDetailSale.setInvoiceDetailMoney(tempInvoiceMoney);
                    invoiceDetailSaleMapper.insert(invoiceDetailSale);

                }
            }else {
//              普通开票，
                BigDecimal invoiceMoney = invoiceBySaleOrder.getInvoiceMoney();
                for (int i = 0; i <saleOrderIdList.size() ; i++) {
                    BigInteger saleOrderId = saleOrderIdList.get(i).getSaleOrderId();
                    QueryWrapper<SaleOrder> saleOrderQueryWrapper = new QueryWrapper<>();
                    saleOrderQueryWrapper.eq("sale_order_id",saleOrderId);
                    SaleOrder saleOrder = saleOrderMapper.selectList(saleOrderQueryWrapper).get(0);
                    BigDecimal shouldInvoiceMoney = saleOrder.getInvoiceMoney();
                    if (shouldInvoiceMoney.compareTo(invoiceMoney)==0){
                        saleOrder.setInvoiceMoney(new BigDecimal("0"));
                        saleOrder.setInvoiceIsSettled(1);
                        saleOrder.setIsInvoice(1);
                        saleOrderMapper.updateById(saleOrder);
                        InvoiceDetailSale invoiceDetailSale = new InvoiceDetailSale();
                        invoiceDetailSale.setInvoiceId(invoiceSaleLsh);
                        invoiceDetailSale.setSaleOrderId(saleOrderId);
                        invoiceDetailSale.setInvoiceDetailMoney(invoiceMoney);
                        invoiceDetailSale.setInvoiceOver(0);
                        invoiceDetailSaleMapper.insert(invoiceDetailSale);
                        break;
                    }else if (shouldInvoiceMoney.compareTo(invoiceMoney)==-1){
                        invoiceMoney=invoiceMoney.subtract(shouldInvoiceMoney);
                        saleOrder.setInvoiceMoney(new BigDecimal("0"));
                        saleOrder.setInvoiceIsSettled(1);
                        saleOrder.setIsInvoice(1);
                        saleOrderMapper.updateById(saleOrder);
                        InvoiceDetailSale invoiceDetailSale = new InvoiceDetailSale();
                        invoiceDetailSale.setInvoiceId(invoiceSaleLsh);
                        invoiceDetailSale.setSaleOrderId(saleOrderId);
                        invoiceDetailSale.setInvoiceDetailMoney(shouldInvoiceMoney);
                        invoiceDetailSale.setInvoiceOver(0);
                        invoiceDetailSaleMapper.insert(invoiceDetailSale);
                        continue;
                    }else if (shouldInvoiceMoney.compareTo(invoiceMoney)==1){
                        saleOrder.setInvoiceMoney(saleOrder.getInvoiceMoney().subtract(invoiceMoney));
                        saleOrder.setIsInvoice(1);
                        saleOrderMapper.updateById(saleOrder);
                        InvoiceDetailSale invoiceDetailSale = new InvoiceDetailSale();
                        invoiceDetailSale.setInvoiceId(invoiceSaleLsh);
                        invoiceDetailSale.setSaleOrderId(saleOrderId);
                        invoiceDetailSale.setInvoiceDetailMoney(invoiceMoney);
                        invoiceDetailSale.setInvoiceOver(0);
                        invoiceDetailSaleMapper.insert(invoiceDetailSale);
                        break;
                    }
                }
            }
        }else {
//                 选择结算完毕的情况
            QueryWrapper<Client> clientQueryWrapper = new QueryWrapper<>();
            clientQueryWrapper.eq("client_id",invoiceBySaleOrder.getClientId());
            Client client = clientMapper.selectList(clientQueryWrapper).get(0);
            BigDecimal invoice1 = client.getInvoice();
            client.setInvoice(invoice1.subtract(invoiceBySaleOrder.getShouldInvoiceMoney()));
            clientMapper.updateById(client);
            List<AccountSpecialSaleOrderIdDetail> saleOrderIdList = invoiceBySaleOrder.getSaleOrderIdList();
            Collections.sort(saleOrderIdList,new SaleOrderByAccountComparator());
            for (int i = 0; i <saleOrderIdList.size() ; i++) {
                BigInteger saleOrderId = saleOrderIdList.get(i).getSaleOrderId();
                QueryWrapper<SaleOrder> saleOrderQueryWrapper = new QueryWrapper<>();
                saleOrderQueryWrapper.eq("sale_order_id",saleOrderId);
                SaleOrder saleOrder = saleOrderMapper.selectList(saleOrderQueryWrapper).get(0);
                BigDecimal invoiceRealMoney = saleOrder.getInvoiceMoney();
                saleOrder.setInvoiceMoney(new BigDecimal("0"));
                saleOrder.setInvoiceIsSettled(1);
                saleOrder.setIsInvoice(1);
                saleOrderMapper.updateById(saleOrder);

                InvoiceDetailSale invoiceDetailSale = new InvoiceDetailSale();
                invoiceDetailSale.setInvoiceId(invoiceSaleLsh);
                invoiceDetailSale.setSaleOrderId(saleOrderId);
                invoiceDetailSale.setInvoiceDetailMoney(invoiceRealMoney);
                invoiceDetailSale.setInvoiceOver(1);
                invoiceDetailSaleMapper.insert(invoiceDetailSale);
            }
        }

        msg.setCode(1);
        msg.setMsg("销售开票操作成功");
        return msg;
    }

    @Override
    public ReturnMsg invoiceKaiping(InvoiceByKaipingOrder invoiceByKaipingOrder, Integer employeeId) {
        ReturnMsg msg = new ReturnMsg();

        //        生成一个invoice数据
        Invoice invoice = new Invoice();
        BigInteger invoiceKaipingLsh = LshUtil.getInvoiceKaiping();
        invoice.setInvoiceId(invoiceKaipingLsh);
        invoice.setEmployeeId(employeeId);
        invoice.setInvoiceTime(invoiceByKaipingOrder.getInvoiceTime());
        invoice.setOperateTime(new Timestamp(new Date().getTime()));
        invoice.setInvoiceType("开平开票");
        invoice.setInvoiceMoney(invoiceByKaipingOrder.getInvoiceMoney());
        invoice.setWho(invoiceByKaipingOrder.getKaipingguyName());
        invoice.setRemark(invoiceByKaipingOrder.getRemark());
        invoice.setInvoiceTitle(invoiceByKaipingOrder.getInvoiceTitle());
        int invoiceInsert = invoiceMapper.insert(invoice);
        if (invoiceInsert==0){
            msg.setCode(0);
            msg.setMsg("开票操作失败,请重试");
            return msg;
        }

//        不抹零的情况
        if (!invoiceByKaipingOrder.getIsOver()){
//            操作开平厂家表
            QueryWrapper<KaipingGuy> kaipingOrderQueryWrapper = new QueryWrapper<>();
            kaipingOrderQueryWrapper.eq("kaipingguy_id",invoiceByKaipingOrder.getKaipingguyId());
            KaipingGuy kaipingGuy = kaipingGuyMapper.selectList(kaipingOrderQueryWrapper).get(0);
            BigDecimal invoice1 = kaipingGuy.getInvoice();
            kaipingGuy.setInvoice(invoice1.subtract(invoiceByKaipingOrder.getInvoiceMoney()));
            kaipingGuyMapper.updateById(kaipingGuy);
//            操作开票细节表和开平订单表
            List<AccountSpecialKaipingOrderIdDetail> kaipingOrderIdList = invoiceByKaipingOrder.getKaipingOrderIdList();
            Collections.sort(kaipingOrderIdList,new KaipingOrderByAccountComparator());
            if (Objects.equals(invoiceByKaipingOrder.getInvoiceMoney(),invoiceByKaipingOrder.getShouldInvoiceMoney())){
//            便捷开票
                for (int i = 0; i <kaipingOrderIdList.size() ; i++) {
//                    操作开平订单
                    BigInteger kaipingOrderId = kaipingOrderIdList.get(i).getKaipingOrderId();
                    QueryWrapper<KaipingOrder> kaipingOrderQueryWrapper1  = new QueryWrapper<>();
                    kaipingOrderQueryWrapper1.eq("kaiping_order_id",kaipingOrderId);
                    KaipingOrder kaipingOrder = kaipingOrderMapper.selectList(kaipingOrderQueryWrapper1).get(0);
                    BigDecimal tempInvoiceMoney = kaipingOrder.getInvoiceMoney();
                    kaipingOrder.setInvoiceMoney(new BigDecimal("0"));
                    kaipingOrder.setInvoiceIsSettled(1);
                    kaipingOrder.setIsInvoice(1);
                    kaipingOrderMapper.updateById(kaipingOrder);
//                    操作开票细节
                    InvoiceDetailKaiping invoiceDetailKaiping = new InvoiceDetailKaiping();
                    invoiceDetailKaiping.setInvoiceId(invoiceKaipingLsh);
                    invoiceDetailKaiping.setKaipingOrderId(kaipingOrderId);
                    invoiceDetailKaiping.setInvoiceOver(0);
                    invoiceDetailKaiping.setInvoiceDetailMoney(tempInvoiceMoney);
                    invoiceDetailKaipingMapper.insert(invoiceDetailKaiping);

                }
            }else {
//              普通开票，
                BigDecimal invoiceMoney = invoiceByKaipingOrder.getInvoiceMoney();
                for (int i = 0; i <kaipingOrderIdList.size() ; i++) {
                    BigInteger kaipingOrderId = kaipingOrderIdList.get(i).getKaipingOrderId();
                    QueryWrapper<KaipingOrder> kaipingOrderQueryWrapper1  = new QueryWrapper<>();
                    kaipingOrderQueryWrapper1.eq("kaiping_order_id",kaipingOrderId);
                    KaipingOrder kaipingOrder = kaipingOrderMapper.selectList(kaipingOrderQueryWrapper1).get(0);
                    BigDecimal shouldInvoiceMoney = kaipingOrder.getInvoiceMoney();
                    if (shouldInvoiceMoney.compareTo(invoiceMoney)==0){
                        kaipingOrder.setInvoiceMoney(new BigDecimal("0"));
                        kaipingOrder.setInvoiceIsSettled(1);
                        kaipingOrder.setIsInvoice(1);
                        kaipingOrderMapper.updateById(kaipingOrder);
                        InvoiceDetailKaiping invoiceDetailKaiping = new InvoiceDetailKaiping();
                        invoiceDetailKaiping.setInvoiceId(invoiceKaipingLsh);
                        invoiceDetailKaiping.setKaipingOrderId(kaipingOrderId);
                        invoiceDetailKaiping.setInvoiceDetailMoney(invoiceMoney);
                        invoiceDetailKaiping.setInvoiceOver(0);
                        invoiceDetailKaipingMapper.insert(invoiceDetailKaiping);
                        break;
                    }else if (shouldInvoiceMoney.compareTo(invoiceMoney)==-1){
                        invoiceMoney=invoiceMoney.subtract(shouldInvoiceMoney);
                        kaipingOrder.setInvoiceMoney(new BigDecimal("0"));
                        kaipingOrder.setInvoiceIsSettled(1);
                        kaipingOrder.setIsInvoice(1);
                        kaipingOrderMapper.updateById(kaipingOrder);
                        InvoiceDetailKaiping invoiceDetailKaiping = new InvoiceDetailKaiping();
                        invoiceDetailKaiping.setInvoiceId(invoiceKaipingLsh);
                        invoiceDetailKaiping.setKaipingOrderId(kaipingOrderId);
                        invoiceDetailKaiping.setInvoiceDetailMoney(shouldInvoiceMoney);
                        invoiceDetailKaiping.setInvoiceOver(0);
                        invoiceDetailKaipingMapper.insert(invoiceDetailKaiping);
                        continue;
                    }else if (shouldInvoiceMoney.compareTo(invoiceMoney)==1){
                        kaipingOrder.setInvoiceMoney(kaipingOrder.getInvoiceMoney().subtract(invoiceMoney));
                        kaipingOrder.setIsInvoice(1);
                        kaipingOrderMapper.updateById(kaipingOrder);
                        InvoiceDetailKaiping invoiceDetailKaiping = new InvoiceDetailKaiping();
                        invoiceDetailKaiping.setInvoiceId(invoiceKaipingLsh);
                        invoiceDetailKaiping.setKaipingOrderId(kaipingOrderId);
                        invoiceDetailKaiping.setInvoiceDetailMoney(invoiceMoney);
                        invoiceDetailKaiping.setInvoiceOver(0);
                        invoiceDetailKaipingMapper.insert(invoiceDetailKaiping);
                        break;
                    }
                }
            }
        }else {
//                 选择结算完毕的情况

            QueryWrapper<KaipingGuy> kaipingGuyQueryWrapper = new QueryWrapper<>();
            kaipingGuyQueryWrapper.eq("kaipingguy_id",invoiceByKaipingOrder.getKaipingguyId());
            KaipingGuy kaipingGuy = kaipingGuyMapper.selectList(kaipingGuyQueryWrapper).get(0);
            BigDecimal invoice1 = kaipingGuy.getInvoice();
            kaipingGuy.setInvoice(invoice1.subtract(invoiceByKaipingOrder.getShouldInvoiceMoney()));
            kaipingGuyMapper.updateById(kaipingGuy);
            List<AccountSpecialKaipingOrderIdDetail> kaipingOrderIdList = invoiceByKaipingOrder.getKaipingOrderIdList();
            Collections.sort(kaipingOrderIdList,new KaipingOrderByAccountComparator());
            for (int i = 0; i <kaipingOrderIdList.size() ; i++) {
                BigInteger kaipingOrderId = kaipingOrderIdList.get(i).getKaipingOrderId();
                QueryWrapper<KaipingOrder> kaipingOrderQueryWrapper = new QueryWrapper<>();
                kaipingOrderQueryWrapper.eq("kaiping_order_id",kaipingOrderId);
                KaipingOrder kaipingOrder = kaipingOrderMapper.selectList(kaipingOrderQueryWrapper).get(0);
                BigDecimal invoiceRealMoney = kaipingOrder.getInvoiceMoney();
                kaipingOrder.setInvoiceMoney(new BigDecimal("0"));
                kaipingOrder.setInvoiceIsSettled(1);
                kaipingOrder.setIsInvoice(1);
                kaipingOrderMapper.updateById(kaipingOrder);

                InvoiceDetailKaiping invoiceDetailKaiping = new InvoiceDetailKaiping();
                invoiceDetailKaiping.setInvoiceId(invoiceKaipingLsh);
                invoiceDetailKaiping.setKaipingOrderId(kaipingOrderId);
                invoiceDetailKaiping.setInvoiceDetailMoney(invoiceRealMoney);
                invoiceDetailKaiping.setInvoiceOver(1);
                invoiceDetailKaipingMapper.insert(invoiceDetailKaiping);
            }
        }

        msg.setCode(1);
        msg.setMsg("开平开票操作成功");
        return msg;
    }




    @Override
    public List<Invoice> getInvoiceData(FindInvoiceDataReception findInvoiceDataReception) {
        Integer now = findInvoiceDataReception.getCurrentPage();
        Integer size = findInvoiceDataReception.getPageSize();
        String duiXiang = findInvoiceDataReception.getDuiXiang();
        String invoiceType = findInvoiceDataReception.getInvoiceType();
        now=(now-1)*size;
        java.sql.Date startTime=null;
        java.sql.Date endTime=null;
        if (Objects.equals(duiXiang, "") ||duiXiang==null){
            duiXiang=null;
        }else {
            duiXiang="%"+duiXiang+"%";
        }

        if (Objects.equals(invoiceType, "所有类型")){
            invoiceType=null;
        }
        if (findInvoiceDataReception.getTimeZone()!=null){
            List<java.sql.Date> timeZone = findInvoiceDataReception.getTimeZone();
            startTime = timeZone.get(0);
            endTime = timeZone.get(1);
        }
        return invoiceMapper.getInvoiceData(now,size,invoiceType,startTime,endTime,duiXiang);
    }

    @Override
    public Integer getInvoiceDataCount(FindInvoiceDataReception findInvoiceDataReception) {
        String duiXiang = findInvoiceDataReception.getDuiXiang();
        String invoiceType = findInvoiceDataReception.getInvoiceType();
        java.sql.Date startTime=null;
        java.sql.Date endTime=null;
        if (Objects.equals(duiXiang, "")){
            duiXiang=null;
        }
        if (Objects.equals(invoiceType, "所有类型")){
            invoiceType=null;
        }
        if (findInvoiceDataReception.getTimeZone()!=null){
            List<java.sql.Date> timeZone = findInvoiceDataReception.getTimeZone();
            startTime = timeZone.get(0);
            endTime = timeZone.get(1);
        }
        return invoiceMapper.getInvoiceDataCount(invoiceType,startTime,endTime,duiXiang);
    }


}
